<?php
if( !defined('BASE') ) exit('Access Denied!');

/**
 * Obullo Framework (c) 2009.
 *
 * PHP5 MVC-min Framework software for PHP 5.2.4 or newer
 * Derived from Code Igniter.
 * 
 * @package         obullo      
 * @author          obullo.com
 * @copyright       Ersin Güvenç (c) 2009.
 * @since           Version 1.0
 * @filesource
 * @license
 */ 

// ------------------------------------------------------------------------

Class CommonException extends Exception {};   

/**
 * Base Front Controller Class
 *
 * Pre-processes for calling controllers
 *
 * @package       Obullo
 * @subpackage    Base
 * @category      Front Controllers
 * @author        Ersin Güvenç
 * @link          
 */
class OB_Front_Controller
{
    /**
    * Constructor
    *
    * Set Front Controller Defaults
    * Suitable for Method Overriding
    * 
    * @see http://www.java2s.com/Code/Php/Class/
    * MethodoverrideforRectangleclass.htm
    * 
    * @access    public
    */
    public function __construct()
    {
        require (BASE.'config'.DIRECTORY_SEPARATOR.'ob_constants'.EXT); 
        require (BASE.'config'.DS.'fl_constants'.EXT);
        require (BASE.'base'.DS.'Errors'.EXT);       
        require (BASE.'base'.DS.'Registry'.EXT); 
        require (BASE.'base'.DS.'Common'.EXT); 
    }
    
    /**
    * Run the Application
    * 
    * @access public
    */
    public function run()
    {
        $Uri    = base_register('URI');
        $Router = base_register('Router');

        header('Content-type: text/html;charset='.config_item('charset')); // UTF-8 
                                        
        $GLOBALS['d']   = $Router->fetch_directory();   // Get requested directory
        $GLOBALS['c']   = $Router->fetch_class();       // Get requested controller
        $GLOBALS['m']   = $Router->fetch_method();      // Get requested method
                                   
        // Check the controller exists or not
        if ( ! file_exists(CONTROLLER.$GLOBALS['d'].DS.'controllers'.DS.$GLOBALS['c'].EXT))
        {
            if($Router->query_string) show_404("{$GLOBALS['c']}/{$GLOBALS['m']}");
            
            throw new CommonException('Unable to load your default controller.
            Please make sure the controller specified in your Routes.php file is valid.');
        }

        require (BASE.'base'.DS.'Ob'.EXT);
        require (BASE.'base'.DS.'Loader'.EXT);
        require (BASE.'base'.DS.'Obullo'.EXT);
        require (BASE.'base'.DS.'Controller'.EXT);
        require (BASE.'base'.DS.'Model'.EXT);

        // call the controller.
        require (CONTROLLER.$GLOBALS['d'].DS.'controllers'.DS.$GLOBALS['c'].EXT);


        if ( ! class_exists($GLOBALS['c'])
            OR $GLOBALS['m'] == 'controller'
            OR strncmp($GLOBALS['m'], '_', 1) == 0
            OR in_array(strtolower($GLOBALS['m']), array_map('strtolower', get_class_methods('Controller')))
            )
        {
            show_404("{$GLOBALS['c']}/{$GLOBALS['m']}");
        }

        // If Everyting ok Declare Called Controller !
        $OB = new $GLOBALS['c']();


        // Check method exist or not
        if ( ! in_array(strtolower($GLOBALS['m']), array_map('strtolower', get_class_methods($OB))))
        {
            show_404("{$GLOBALS['c']}/{$GLOBALS['m']}"); 
        }

        // Call the requested method.                1       2       3
        // Any URI segments present (besides the directory/class/function) 
        // will be passed to the method for convenience
        call_user_func_array(array($OB, $GLOBALS['m']), array_slice($Uri->rsegments, 3));


        // Close the pdo connection ..
        if (class_exists('DB') AND isset($OB->db))
        $OB->db = NULL;
        
        
        // Close your custom db connection if its exists !
        // ..
        // .
        
    }
    
}  // end class.

/*
|--------------------------------------------------------------------------
| Custom User Front Controller
|--------------------------------------------------------------------------
|
| User can create own Front Controller who want extend
| and do method overridding for base OB_Front_Controller library
|
*/
require('application'.DIRECTORY_SEPARATOR.'user'.DIRECTORY_SEPARATOR.'Front_Controller'.EXT);


// END Front Controller class

/* End of file Front_Controller.php */
/* Location: ./base/base/Front_Controller.php */

?>
